package net.kelissa.jenetic.example.salesman;

import java.util.List;

import net.kelissa.jenetic.Gene;
import net.kelissa.jenetic.Fitner;

public class FitnerSalesman implements Fitner
{

  @Override
  public double fitness(List<Gene> genes)
  {
    double fitness = 0.;
    for (int i = 0; i < genes.size() - 1; i++)
    {
      GeneSalesman gene1 = (GeneSalesman) genes.get(i);
      GeneSalesman gene2 = (GeneSalesman) genes.get(i+1);
      double[] position1 = gene1.getValue().getPosition();
      double[] position2 = gene2.getValue().getPosition();
      double x1 = position1[0];
      double x2 = position2[0];
      double y1 = position1[1];
      double y2 = position2[1];
      fitness += Math.sqrt( ((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) );

    }
    return -1 * fitness;
  }

}
